//
//  OIDHistoryViewController.m
//  EducationOID
//
//  Created by slz on 2017/6/30.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OIDHistoryViewController.h"
#import "OIDHistoryTableViewCell.h"
#import "NSDate+QA.h"
#import "NSString+OIDAnalysis.h"
#import "OIQRScanResultViewController.h"
#import "OIDInfoRequestModel.h"
#import "NSURL+QueryDict.h"


@interface OIDHistoryViewController ()<UITableViewDelegate,UITableViewDataSource,MFWDataObserver>
@property (nonatomic,strong) YWTableView *tableView;
@end

@implementation OIDHistoryViewController
{
    OIDInfoRequestModel * _oidModel;
    UILabel * _tipLabel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.showNavigationBar = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;
    if (self.sourceData && self.sourceData.count > 0){
        [self initTableView];
    }else{
        [self initTipLabel];
    }
    
    switch (_type) {
        case OIDHistoryCreat:
            [self setControllerTitle:@"生成码历史记录"];
            break;
        case OIDHistoryAnysis:
            [self setControllerTitle:@"解码历史记录"];
            break;
        case OIDHistoryCollect:
            [self setControllerTitle:@"收藏记录"];
            break;
        default:
            break;
    }

    // Do any additional setup after loading the view.
}


- (void)initTableView{
    self.tableView = [YWTableView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
}

-(void)initTipLabel{
    UILabel * tipLabel = [UILabel new];
    tipLabel.fontSize = 15;
    tipLabel.textColor = kDark_TextColor_Default;
    tipLabel.textAlignment = NSTextAlignmentCenter;
    [[self view] addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.centerY.mas_equalTo(self.view.mas_centerY);
    }];
    switch (_type) {
        case OIDHistoryCreat:
            tipLabel.text = @"还没有生成码历史记录~";
            break;
        case OIDHistoryAnysis:
            tipLabel.text = @"还没有解码历史记录~";
            break;
        case OIDHistoryCollect:
            tipLabel.text = @"还没有收藏记录~";
            break;
        default:
            break;
    }
}


- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _sourceData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * reuserIndentifier = @"OIDHistoryTableViewCell";
    OIDHistoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuserIndentifier];
    if (!cell){
        cell = [[OIDHistoryTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIndentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary * dict = _sourceData[indexPath.row];
    cell.nameLabel.text = [dict stringValueForKey:@"url"];
    cell.timeLabel.text = [NSDate timeStringWithDateFormatter:[dict stringValueForKey:@"time"]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * urlString = [_sourceData[indexPath.row] stringValueForKey:@"url"];
    BOOL isUrl = [urlString isUrl];
    
    if (isUrl)
    {
        NSURL * url = [NSURL URLWithString:urlString];
        NSString * oidString = [url queryValueWithKey:@"oid"];
        OIQRScanResultViewController * resultController = [[OIQRScanResultViewController alloc]init];
        resultController.resultSuccessUrl = urlString;
        resultController.isQrcode = NO;

        if (oidString && oidString.length > 0){
            resultController.isOidValid = YES;

        }else{
            resultController.isOidValid = NO;
        }
        [self.navigationController pushViewController:resultController animated:YES];
    }else{
        [self.view showTip:@"error"];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
